import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

public class Filter {
    public static void main(String[] args) {
        // test3();
        test4();
    }

    public static boolean isEmpty(Collection coll) {
        return coll == null || coll.isEmpty();
    }

    public static boolean isNotEmpty(Collection coll) {
        return !isEmpty(coll);
    }

    public static void test1() {
        List<String> list = null;
        Optional<List<String>> optional = Optional.ofNullable(list).filter(Filter::isNotEmpty);
        System.out.println(optional.isPresent()); // 输出: false
    }

    public static void test2() {
        List<String> list2 = new ArrayList<>();
        Optional<List<String>> optional2 = Optional.ofNullable(list2).filter(Filter::isNotEmpty);
        System.out.println(optional2.isPresent()); // 输出: false
    }

    public static void test3() {
        List<String> list = Arrays.asList("a", "b", "c");
        Optional<List<String>> optional = Optional.ofNullable(list).filter(Filter::isNotEmpty);

        System.out.println(optional.isPresent()); // 输出: true
        System.out.println(optional.get()); // 输出: [a, b, c]
    }

    public static void test4() {
        Optional.ofNullable(createStudent(20)).filter(stu-> stu.getAge() > 18).ifPresent(stu-> System.out.println(stu.getName()));
    }

    private static Student createStudent(Integer age) {
        System.out.println("create student");
        return new Student(1 + age, "xxt" + age, age);
    }
}
